<?php
// View for admin - authors users
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT .'controller/form.inc.php';
require_once WACT_ROOT.'view/view.inc.php';
require_once WACT_ROOT .'view/formview.inc.php';

require_once APP_ROOT . 'view/common/layout.formview.php';

require_once 'model/common/user.php';
require_once 'model/common/state.php';
require_once 'model/common/categories.php';

class AuthorUsersListView extends LayoutFormView {

    function AuthorUsersListView($TemplateFile) {
        parent :: LayoutFormView($TemplateFile);
    }


    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare($controller, $request, $responseModel);
          
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
 

		$pager =& $this->Template->getChild('pagenav');
        $list =& User::getAuthorsUsersList($pager);

        $list_updated=array();


        $list->reset();
        while ($list->next()) {
        	// adjust active property and photo
        	$dataspace = $list->export();

                $user_id = $list->get('user_id');
        		$active = $list->get('active');

        		if ($active>0) {
         			$dataspace['active_show'] = 1;
         		}
                //print_r($dataspace);


                $staff = $list->get('staff');
                if ($staff>0) {
                    $dataspace['staff_show'] = 1;
        		}

        		//print_r($dataspace);
                $list_updated[$user_id] = $dataspace;
        }
        //print_r($data);

        $list_source_updated =& new ArrayDataSet($list_updated);
        //print_r($list_updated);

        $this->Template->setChildDataSource('List', $list_source_updated);

        $active = $responseModel->get('active');
        if ($active>0) $responseModel->set('active_show',1);

        //load width and height of the image
        $photo_url = $responseModel->get('photo_url');
        //$photo_url = str_replace(' ','%20',$photo_url); //REPLACE THE SPACES  
        list($width, $height, $type, $attr) = @getimagesize(HOST.$photo_url);
        //print HOST.$photo_url.$width.$height;
        $responseModel->set('photo_width', $width);
        $responseModel->set('photo_height', $height);

        //$photo_id = $responseModel->get('photo_id');

        //if ($photo_id>0) {
           //$dataspace = array_merge($dataspace,array('photo_show'=>1));
           //$responseModel->set('photo_show',1);
           //$path = $responseModel->get('path');
           //$fname = $responseModel->get('filename_local');
           //$fname_thumb = $responseModel->get('filename_thumb');

           //$photo_src = "/".APP_NAME."/".$path."/".$fname;
           //$photo_src_thumb = "/".APP_NAME."/".$path."/".$fname_thumb;
           //$responseModel->set('image_path',$photo_src);
           //$responseModel->set('image_path_thumb',$photo_src_thumb);
       //}

        //print_r($responseModel);

        // populate states
        $state = $responseModel->get('state');
        $states =& $this->Template->getChild('state');
        $states_list =& State::getStates();
        $states->setChoices($states_list);
        $states->setSelection($state);

        //populate author titles
        $title = $responseModel->get('title');
        //print 'Title '.$title;
        $titles =& $this->Template->getChild('title');
        $titles_list = array('Associate Editor'=>'Associate Editor',
        					 'Columnist'=>'Columnist',
        					 'Editor'=>'Editor',
        					 'Fan Correspondent'=>'Fan Correspondent',
        					 'Magazine Staff'=>'Magazine Staff',
                             'Managing Editor'=>'Managing Editor',
        					 'Photographer'=>'Photographer',
        					 'Publisher'=>'Publisher',
                             'Recruiting analyst'=>'Recruiting analyst',
                             'Recruiting editor'=>'Recruiting editor',
                             'Senior Recruiter'=>'Senior Recruiter',
                             'Senior Writer'=>'Senior Writer',
                             'Special to'=>'Special to',
                             'Staff'=>'Staff',
                             'Staff writer'=>'Staff writer'
        					 );
    	$titles->setChoices($titles_list);
    	$titles->setSelection($title );

    	//populate categories permissions
        $categories =& $this->Template->getChild('categories');
        $categories_list =& CategoriesModel::getSportCategories(true,false);
        $categories->setChoices($categories_list);


        //populate premium categories permissions
        $premium_categories =& $this->Template->getChild('premium');
        $premium_categories_list =& CategoriesModel::getSportCategories(false,true);
        $premium_categories->setChoices($premium_categories_list);

        $user_id = $responseModel->get('user_id');
        $normal = array();
        $premium = array();
     	User::loadAuthorUserPermissions($user_id, $normal, $premium);

     	//print_r($normal);
     	if (count($normal)>0) $categories->setSelection($normal);
        //print_r($premium);
        if (count($premium)>0) $premium_categories->setSelection($premium);


    }


}



?>